home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / REGSETUP.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  1999-07-21  |  25.4 KB  |  548 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. error = 'Registry Setup Error'
  5. import sys
  6.  
  7. def FileExists(fname):
  8.     '''Check if a file exists.  Returns true or false.
  9. \t'''
  10.     import os
  11.     
  12.     try:
  13.         os.stat(fname)
  14.         return 1
  15.     except os.error:
  16.         details = None
  17.         return 0
  18.  
  19.  
  20.  
  21. def IsPackageDir(path, packageName, knownFileName):
  22.     '''Given a path, a ni package name, and possibly a known file name in
  23.            the root of the package, see if this path is good.
  24.       '''
  25.     import os
  26.     if knownFileName is None:
  27.         knownFileName = '.'
  28.     
  29.     return FileExists(os.path.join(os.path.join(path, packageName), knownFileName))
  30.  
  31.  
  32. def FindPackagePath(packageName, knownFileName, searchPaths):
  33.     '''Find a package.
  34.  
  35.            Given a ni style package name, check the package is registered.
  36.  
  37.            First place looked is the registry for an existing entry.  Then
  38.            the searchPaths are searched.
  39.       '''
  40.     import win32api
  41.     import win32con
  42.     import regutil
  43.     pathLook = regutil.GetRegisteredNamedPath(packageName)
  44.     if pathLook and IsPackageDir(pathLook, packageName, knownFileName):
  45.         return (pathLook, None)
  46.     
  47.     for pathLook in searchPaths:
  48.         pass
  49.     
  50.     raise error, 'The package %s can not be located' % packageName
  51.  
  52.  
  53. def FindHelpPath(helpFile, helpDesc, searchPaths):
  54.     import win32api
  55.     import win32con
  56.     import os
  57.     
  58.     try:
  59.         key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows\\Help', 0, win32con.KEY_ALL_ACCESS)
  60.         
  61.         try:
  62.             
  63.             try:
  64.                 path = win32api.RegQueryValueEx(key, helpDesc)[0]
  65.                 print 'Looking in', path
  66.                 if FileExists(os.path.join(path, helpFile)):
  67.                     return win32api.GetFullPathName(path)
  68.             except win32api.error:
  69.                 pass
  70.  
  71.         finally:
  72.             win32api.RegCloseKey(key)
  73.  
  74.     except win32api.error:
  75.         pass
  76.  
  77.     for pathLook in searchPaths:
  78.         pathLook = os.path.join(pathLook, 'Help')
  79.         if FileExists(os.path.join(pathLook, helpFile)):
  80.             return win32api.GetFullPathName(pathLook)
  81.         
  82.     
  83.     raise error, 'The help file %s can not be located' % helpFile
  84.  
  85.  
  86. def FindAppPath(appName, knownFileName, searchPaths):
  87.     '''Find an application.
  88.  
  89.          First place looked is the registry for an existing entry.  Then
  90.          the searchPaths are searched.
  91.       '''
  92.     import win32api
  93.     import win32con
  94.     import regutil
  95.     import string
  96.     import os
  97.     regPath = regutil.GetRegisteredNamedPath(appName)
  98.     if regPath:
  99.         pathLook = string.split(regPath, ';')[0]
  100.     
  101.     if regPath and FileExists(os.path.join(pathLook, knownFileName)):
  102.         return None
  103.     
  104.     for pathLook in searchPaths:
  105.         pass
  106.     
  107.     raise error, 'The file %s can not be located for application %s' % (knownFileName, appName)
  108.  
  109.  
  110. def FindRegisteredModule(moduleName, possibleRealNames, searchPaths):
  111.     '''Find a registered module.
  112.  
  113.          First place looked is the registry for an existing entry.  Then
  114.          the searchPaths are searched.
  115.          
  116. \t   Returns the full path to the .exe or None if the current registered entry is OK.
  117.       '''
  118.     import win32api
  119.     import win32con
  120.     import regutil
  121.     import string
  122.     
  123.     try:
  124.         fname = win32api.RegQueryValue(regutil.GetRootKey(), regutil.BuildDefaultPythonKey() + '\\Modules\\%s' % moduleName)
  125.         if FileExists(fname):
  126.             return None
  127.     except win32api.error:
  128.         pass
  129.  
  130.     return LocateFileName(possibleRealNames, searchPaths)
  131.  
  132.  
  133. def FindPythonExe(exeAlias, possibleRealNames, searchPaths):
  134.     '''Find an exe.
  135.  
  136.          First place looked is the registry for an existing entry.  Then
  137.          the searchPaths are searched.
  138.          
  139. \t   Returns the full path to the .exe, and a boolean indicating if the current 
  140. \t   registered entry is OK.
  141.       '''
  142.     import win32api
  143.     import win32con
  144.     import regutil
  145.     import string
  146.     if possibleRealNames is None:
  147.         possibleRealNames = exeAlias
  148.     
  149.     
  150.     try:
  151.         fname = win32api.RegQueryValue(regutil.GetRootKey(), regutil.GetAppPathsKey() + '\\' + exeAlias)
  152.         if FileExists(fname):
  153.             return (fname, 1)
  154.     except win32api.error:
  155.         pass
  156.  
  157.     return (LocateFileName(possibleRealNames, searchPaths), 0)
  158.  
  159.  
  160. def QuotedFileName(fname):
  161.     '''Given a filename, return a quoted version if necessary
  162.       '''
  163.     import win32api
  164.     import win32con
  165.     import regutil
  166.     import string
  167.     
  168.     try:
  169.         string.index(fname, ' ')
  170.         return '"%s"' % fname
  171.     except ValueError:
  172.         return fname
  173.  
  174.  
  175.  
  176. def LocateFileName(fileNamesString, searchPaths):
  177.     '''Locate a file name, anywhere on the search path.
  178.  
  179. \t   If the file can not be located, prompt the user to find it for us
  180. \t   (using a common OpenFile dialog)
  181.  
  182. \t   Raises KeyboardInterrupt if the user cancels.
  183. \t'''
  184.     import win32api
  185.     import win32con
  186.     import regutil
  187.     import string
  188.     import os
  189.     fileNames = string.split(fileNamesString, ';')
  190.     for path in searchPaths:
  191.         for fileName in fileNames:
  192.             
  193.             try:
  194.                 retPath = os.path.join(path, fileName)
  195.                 os.stat(retPath)
  196.             except os.error:
  197.                 0
  198.                 0
  199.                 fileNames
  200.                 retPath = None
  201.             except:
  202.                 0
  203.  
  204.         
  205.     else:
  206.         fileName = fileNames[0]
  207.         
  208.         try:
  209.             import win32ui
  210.         except ImportError:
  211.             None if retPath else searchPaths
  212.             None if retPath else searchPaths
  213.             raise error, 'Need to locate the file %s, but the win32ui module is not available\nPlease run the program again, passing as a parameter the path to this file.' % fileName
  214.         except:
  215.             None if retPath else searchPaths
  216.  
  217.         flags = win32con.OFN_FILEMUSTEXIST
  218.         ext = os.path.splitext(fileName)[1]
  219.         filter = 'Files of requested type (*%s)|*%s||' % (ext, ext)
  220.         dlg = win32ui.CreateFileDialog(1, None, fileName, flags, filter, None)
  221.         if dlg.DoModal() != win32con.IDOK:
  222.             raise KeyboardInterrupt, 'User cancelled the process'
  223.         
  224.         retPath = dlg.GetPathName()
  225.     return win32api.GetFullPathName(retPath)
  226.  
  227.  
  228. def LocatePath(fileName, searchPaths):
  229.     '''Like LocateFileName, but returns a directory only.
  230. \t'''
  231.     import os
  232.     import win32api
  233.     return win32api.GetFullPathName(os.path.split(LocateFileName(fileName, searchPaths))[0])
  234.  
  235.  
  236. def LocateOptionalPath(fileName, searchPaths):
  237.     '''Like LocatePath, but returns None if the user cancels.
  238. \t'''
  239.     
  240.     try:
  241.         return LocatePath(fileName, searchPaths)
  242.     except KeyboardInterrupt:
  243.         return None
  244.  
  245.  
  246.  
  247. def LocateOptionalFileName(fileName, searchPaths = None):
  248.     '''Like LocateFileName, but returns None if the user cancels.
  249. \t'''
  250.     
  251.     try:
  252.         return LocateFileName(fileName, searchPaths)
  253.     except KeyboardInterrupt:
  254.         return None
  255.  
  256.  
  257.  
  258. def LocatePythonCore(searchPaths):
  259.     '''Locate and validate the core Python directories.  Returns a list
  260.          of paths that should be used as the core (ie, un-named) portion of
  261.          the Python path.
  262. \t'''
  263.     import win32api
  264.     import win32con
  265.     import string
  266.     import os
  267.     import regutil
  268.     currentPath = regutil.GetRegisteredNamedPath(None)
  269.     if currentPath:
  270.         presearchPaths = string.split(currentPath, ';')
  271.     else:
  272.         presearchPaths = [
  273.             win32api.GetFullPathName('.')]
  274.     libPath = None
  275.     for path in presearchPaths:
  276.         pass
  277.     
  278.     if libPath is None and searchPaths is not None:
  279.         libPath = LocatePath('os.py', searchPaths)
  280.     
  281.     if libPath is None:
  282.         raise error, 'The core Python library could not be located.'
  283.     
  284.     corePath = None
  285.     for path in presearchPaths:
  286.         pass
  287.     
  288.     if corePath is None and searchPaths is not None:
  289.         corePath = LocatePath('parser.dll', searchPaths)
  290.     
  291.     if corePath is None:
  292.         raise error, 'The core Python path could not be located.'
  293.     
  294.     installPath = win32api.GetFullPathName(os.path.join(libPath, '..'))
  295.     return (installPath, [
  296.         libPath,
  297.         corePath])
  298.  
  299.  
  300. def FindRegisterPackage(packageName, knownFile, searchPaths):
  301.     '''Find and Register a package.
  302.  
  303. \t   Assumes the core registry setup correctly.
  304.  
  305. \t   In addition, if the location located by the package is already
  306.            in the **core** path, then an entry is registered, but no path.
  307. \t   (no other paths are checked, as the application whose path was used
  308. \t   may later be uninstalled.  This should not happen with the core)
  309. \t'''
  310.     import win32api
  311.     import win32con
  312.     import regutil
  313.     import string
  314.     if not packageName:
  315.         raise error, 'A package name must be supplied'
  316.     
  317.     corePaths = string.split(regutil.GetRegisteredNamedPath(None), ';')
  318.     if not searchPaths:
  319.         searchPaths = corePaths
  320.     
  321.     
  322.     try:
  323.         (pathLook, pathAdd) = FindPackagePath(packageName, knownFile, searchPaths)
  324.         if pathAdd is not None:
  325.             if pathAdd in corePaths:
  326.                 pathAdd = ''
  327.             
  328.             regutil.RegisterNamedPath(packageName, pathAdd)
  329.         
  330.         return pathLook
  331.     except error:
  332.         details = None
  333.         print '*** The %s package could not be registered - %s' % (packageName, details)
  334.         print '*** Please ensure you have passed the correct paths on the command line.'
  335.         print '*** - For packages, you should pass a path to the packages parent directory,'
  336.         print '*** - and not the package directory itself...'
  337.  
  338.  
  339.  
  340. def FindRegisterApp(appName, knownFiles, searchPaths):
  341.     '''Find and Register a package.
  342.  
  343. \t   Assumes the core registry setup correctly.
  344.  
  345. \t'''
  346.     import win32api
  347.     import win32con
  348.     import regutil
  349.     import string
  350.     if type(knownFiles) == type(''):
  351.         knownFiles = [
  352.             knownFiles]
  353.     
  354.     paths = []
  355.     
  356.     try:
  357.         for knownFile in knownFiles:
  358.             pathLook = FindAppPath(appName, knownFile, searchPaths)
  359.     except error:
  360.         details = None
  361.         print '*** ', details
  362.         return None
  363.  
  364.     regutil.RegisterNamedPath(appName, string.join(paths, ';'))
  365.  
  366.  
  367. def FindRegisterModule(modName, actualFileNames, searchPaths):
  368.     '''Find and Register a module.
  369.  
  370. \t   Assumes the core registry setup correctly.
  371. \t'''
  372.     import regutil
  373.     
  374.     try:
  375.         fname = FindRegisteredModule(modName, actualFileNames, searchPaths)
  376.         if fname is not None:
  377.             regutil.RegisterModule(modName, fname)
  378.     except error:
  379.         details = None
  380.         print '*** ', details
  381.  
  382.  
  383.  
  384. def FindRegisterPythonExe(exeAlias, searchPaths, actualFileNames = None):
  385.     '''Find and Register a Python exe (not necessarily *the* python.exe)
  386.  
  387. \t   Assumes the core registry setup correctly.
  388. \t'''
  389.     import win32api
  390.     import win32con
  391.     import regutil
  392.     import string
  393.     (fname, ok) = FindPythonExe(exeAlias, actualFileNames, searchPaths)
  394.     if not ok:
  395.         regutil.RegisterPythonExe(fname, exeAlias)
  396.     
  397.     return fname
  398.  
  399.  
  400. def FindRegisterHelpFile(helpFile, searchPaths, helpDesc = None):
  401.     import regutil
  402.     
  403.     try:
  404.         pathLook = FindHelpPath(helpFile, helpDesc, searchPaths)
  405.     except error:
  406.         details = None
  407.         print '*** ', details
  408.         return None
  409.  
  410.     regutil.RegisterHelpFile(helpFile, pathLook, helpDesc)
  411.  
  412.  
  413. def SetupCore(searchPaths):
  414.     '''Setup the core Python information in the registry.
  415.  
  416. \t   This function makes no assumptions about the current state of sys.path.
  417.  
  418. \t   After this function has completed, you should have access to the standard
  419. \t   Python library, and the standard Win32 extensions
  420. \t'''
  421.     import sys
  422.     for path in searchPaths:
  423.         sys.path.append(path)
  424.     
  425.     import string
  426.     import os
  427.     import regutil
  428.     import win32api
  429.     import win32con
  430.     (installPath, corePaths) = LocatePythonCore(searchPaths)
  431.     print corePaths
  432.     regutil.RegisterNamedPath(None, string.join(corePaths, ';'))
  433.     hKey = win32api.RegCreateKey(regutil.GetRootKey(), regutil.BuildDefaultPythonKey())
  434.     
  435.     try:
  436.         win32api.RegSetValue(hKey, 'InstallPath', win32con.REG_SZ, installPath)
  437.     finally:
  438.         win32api.RegCloseKey(hKey)
  439.  
  440.     regutil.RegisterCoreDLL()
  441.     win32paths = win32api.GetFullPathName(os.path.split(win32api.__file__)[0]) + ';' + win32api.GetFullPathName(os.path.split(LocateFileName('win32con.py;win32con.pyc', sys.path))[0])
  442.     FindRegisterModule('pywintypes', 'pywintypes15.dll', [
  443.         '.',
  444.         win32api.GetSystemDirectory()])
  445.     regutil.RegisterNamedPath('win32', win32paths)
  446.  
  447.  
  448. def RegisterShellInfo(searchPaths):
  449.     '''Registers key parts of the Python installation with the Windows Shell.
  450.  
  451. \t   Assumes a valid, minimal Python installation exists
  452. \t   (ie, SetupCore() has been previously successfully run)
  453. \t'''
  454.     import regutil
  455.     import win32con
  456.     exePath = FindRegisterPythonExe('Python.exe', searchPaths)
  457.     regutil.SetRegistryDefaultValue('.py', 'Python.File', win32con.HKEY_CLASSES_ROOT)
  458.     regutil.RegisterShellCommand('Open', QuotedFileName(exePath) + ' %1 %*', '&Run')
  459.     regutil.SetRegistryDefaultValue('Python.File\\DefaultIcon', '%s,0' % exePath, win32con.HKEY_CLASSES_ROOT)
  460.     FindRegisterHelpFile('Python.hlp', searchPaths, 'Main Python Documentation')
  461.  
  462.  
  463. def RegisterPythonwin(searchPaths):
  464.     '''Knows how to register Pythonwin components
  465. \t'''
  466.     import regutil
  467.     FindRegisterApp('Pythonwin', 'docview.py', searchPaths)
  468.     FindRegisterHelpFile('Pythonwin.hlp', searchPaths, 'Pythonwin Reference')
  469.     FindRegisterPythonExe('pythonwin.exe', searchPaths, 'Pythonwin.exe')
  470.     regutil.RegisterShellCommand('Edit', QuotedFileName('pythonwin.exe') + ' /edit %1')
  471.     regutil.RegisterDDECommand('Edit', 'Pythonwin', 'System', '[self.OpenDocumentFile(r"%1")]')
  472.     FindRegisterModule('win32ui', 'win32ui.pyd', searchPaths)
  473.     FindRegisterModule('win32uiole', 'win32uiole.pyd', searchPaths)
  474.     fnamePythonwin = regutil.GetRegisteredExe('Pythonwin.exe')
  475.     fnamePython = regutil.GetRegisteredExe('Python.exe')
  476.     regutil.RegisterFileExtensions(defPyIcon = fnamePythonwin + ',0', defPycIcon = fnamePythonwin + ',5', runCommand = QuotedFileName(fnamePython) + ' %1 %*')
  477.  
  478.  
  479. def UnregisterPythonwin():
  480.     '''Knows how to unregister Pythonwin components
  481. \t'''
  482.     import regutil
  483.     regutil.UnregisterNamedPath('Pythonwin')
  484.     regutil.UnregisterHelpFile('Pythonwin.hlp')
  485.     regutil.UnregisterPythonExe('pythonwin.exe')
  486.     regutil.UnregisterModule('win32ui')
  487.     regutil.UnregisterModule('win32uiole')
  488.  
  489.  
  490. def RegisterWin32com(searchPaths):
  491.     '''Knows how to register win32com components
  492. \t'''
  493.     import win32api
  494.     corePath = FindRegisterPackage('win32com', 'olectl.py', searchPaths)
  495.     if corePath:
  496.         FindRegisterHelpFile('win32com.hlp', searchPaths + [
  497.             corePath + '\\win32com'], 'Python COM Reference')
  498.         FindRegisterModule('pythoncom', 'pythoncom15.dll', [
  499.             win32api.GetSystemDirectory(),
  500.             '.'])
  501.     
  502.  
  503. usage = 'regsetup.py - Setup/maintain the registry for Python apps.\n\nRun without options, (but possibly search paths) to repair a totally broken\npython registry setup.  This should allow other options to work.\n\nUsage:   %s [options ...] paths ...\n-p packageName  -- Find and register a package.  Looks in the paths for\n                   a sub-directory with the name of the package, and\n                   adds a path entry for the package.\n-a appName      -- Unconditionally add an application name to the path.\n                   A new path entry is create with the app name, and the\n                   paths specified are added to the registry.\n-c              -- Add the specified paths to the core Pythonpath.\n                   If a path appears on the core path, and a package also \n                   needs that same path, the package will not bother \n                   registering it.  Therefore, By adding paths to the \n                   core path, you can avoid packages re-registering the same path.  \n-m filename     -- Find and register the specific file name as a module.\n                   Do not include a path on the filename!\n--shell         -- Register everything with the Win95/NT shell.\n--pythonwin     -- Find and register all Pythonwin components.\n--unpythonwin   -- Unregister Pythonwin\n--win32com      -- Find and register all win32com components\n--upackage name -- Unregister the package\n--uapp name     -- Unregister the app (identical to --upackage)\n--umodule name  -- Unregister the module\n\n--description   -- Print a description of the usage.\n--examples      -- Print examples of usage.\n' % sys.argv[0]
  504. description = 'If no options are processed, the program attempts to validate and set \nthe standard Python path to the point where the standard library is\navailable.  This can be handy if you move Python to a new drive/sub-directory,\nin which case most of the options would fail (as they need at least string.py,\nos.py etc to function.)\nRunning without options should repair Python well enough to run with \nthe other options.\n\npaths are search paths that the program will use to seek out a file.\nFor example, when registering the core Python, you may wish to\nprovide paths to non-standard places to look for the Python help files,\nlibrary files, etc.  When registering win32com, you should pass paths\nspecific to win32com.\n\nSee also the "regcheck.py" utility which will check and dump the contents\nof the registry.\n'
  505. examples = 'Examples:\n"regsetup c:\\wierd\\spot\\1 c:\\wierd\\spot\\2"\nAttempts to setup the core Python.  Looks in some standard places,\nas well as the 2 wierd spots to locate the core Python files (eg, Python.exe,\npython14.dll, the standard library and Win32 Extensions.\n\n"regsetup --win32com"\nAttempts to register win32com.  No options are passed, so this is only\nlikely to succeed if win32com is already successfully registered, or the\nwin32com directory is current.  If neither of these are true, you should pass\nthe path to the win32com directory.\n\n"regsetup -a myappname . .\\subdir"\nRegisters a new Pythonpath entry named myappname, with "C:\\I\\AM\\HERE" and\n"C:\\I\\AM\\HERE\\subdir" added to the path (ie, all args are converted to\nabsolute paths)\n\n"regsetup -c c:\\my\\python\\files"\nUnconditionally add "c:\\my\\python\\files" to the \'core\' Python path.\n\n"regsetup -m some.pyd \\windows\\system"\nRegister the module some.pyd in \\windows\\system as a registered\nmodule.  This will allow some.pyd to be imported, even though the\nwindows system directory is not (usually!) on the Python Path.\n\n"regsetup --umodule some"\nUnregister the module "some".  This means normal import rules then apply\nfor that module.\n'
  506. if __name__ == '__main__':
  507.     if len(sys.argv) > 1 and sys.argv[1] in [
  508.         '/?',
  509.         '-?',
  510.         '-help',
  511.         '-h']:
  512.         print usage
  513.     elif len(sys.argv) == 1 or not (sys.argv[1][0] in [
  514.         '/',
  515.         '-']):
  516.         searchPath = sys.path[:]
  517.         for arg in sys.argv[1:]:
  518.             searchPath.append(arg)
  519.         
  520.         searchPath.append('..\\..')
  521.         print 'Attempting to setup/repair the Python core'
  522.         SetupCore(searchPath)
  523.         RegisterShellInfo(searchPath)
  524.         print 'Registration complete - checking the registry...'
  525.         import regcheck
  526.         regcheck.CheckRegistry()
  527.     else:
  528.         searchPaths = []
  529.         import getopt
  530.         import string
  531.         (opts, args) = getopt.getopt(sys.argv[1:], 'p:a:m:c', [
  532.             'pythonwin',
  533.             'unpythonwin',
  534.             'win32com',
  535.             'shell',
  536.             'upackage=',
  537.             'uapp=',
  538.             'umodule=',
  539.             'description',
  540.             'examples'])
  541.         for arg in args:
  542.             searchPaths.append(arg)
  543.         
  544.         for o, a in opts:
  545.             pass
  546.         
  547.  
  548.